﻿package src {
	
	import flash.display.*;
	
	/**
	 * Represent the .matches file
	 * @author Indrek Tamming
	 */
	public class MotifMatches {
		
		/**
		 * An array of Match files
		 */
		private var motifMatchArray:Array;
		
		/**
		 * The Motif
		 */
		private var motif:Motif;
		
		/**
		 * Matches color
		 */
		private var color:uint = 0;
		
		/**
		 * Filter value
		 */
		private var scoreFilter:Number;
		
		/**
		 * True if motif is hidden
		 */
		private var hide:Boolean = false;
		
		/**
		 * minimum score
		 */
		private var minScore:Number = 1000;
		
		/**
		 * maximum score
		 */
		private var maxScore:Number = -1000;
		
		/**
		 * Init the MotifMatches object
		 * @param	matchesFile	-	 .matches file as String
		 */
		public function MotifMatches(matchesFile:String) {
			var rows:Array = matchesFile.split("\n");
			motifMatchArray = new Array(rows.length);
			for (var i:int = 0 ; i < rows.length; i++) {
				var columns:Array = rows[i].split("\t");
				if (columns.length < 5) continue;
				var match:MotifMatch = new MotifMatch(columns[0], columns[1], columns[2], columns[3], columns[4]);
				calculateMinMax(columns[4]);
				motifMatchArray[i] = match;
			}
			this.scoreFilter = minScore;
		}
		
		/**
		 * Calculate min and max values
		 * @param	score
		 */
		private function calculateMinMax(score:Number):void {
			if (score < minScore) minScore = score;
			else if (score > maxScore) maxScore = score;
		}
		
		/**
		 * Get all matches
		 * @return	-	 array of MotifMatch objects
		 */
		public function getAllMatches():Array {
			return motifMatchArray;
		}
		
		/**
		 * Get the Motif object
		 * @return
		 */
		public function getMotif():Motif {
			return motif;
		}
		
		/**
		 * Set the Motif object
		 * @param	motif
		 */
		public function setMotif(motif:Motif):void {
			this.motif = motif;
		}
		
		/**
		 * Get the filter value
		 * @return
		 */
		public function getFilter():Number {
			if (hide) return maxScore + 5;
			else return scoreFilter;
		}
		
		/**
		 * Set the filter value
		 * @param	value
		 */
		public function setFilter(value:Number):void {
			this.scoreFilter = value;
		}
		
		/**
		 * Get color
		 * @return
		 */
		public function getColor():uint {
			return color;
		}
		
		/**
		 * Set color
		 * @param	value
		 */
		public function setColor(value:uint):void {
			this.color = value;
		}
		
		/**
		 * Get minimum score
		 * @return
		 */
		public function getMinScore():Number {
			return minScore;
		}
		
		/**
		 * Get maximum score
		 * @return
		 */
		public function getMaxScore():Number {
			return maxScore;
		}
		
		/**
		 * Set motif hidden
		 * @param	value
		 */
		public function setHide(value:Boolean):void {
			this.hide = value;
		}
		
		/**
		 * Check if motif is hidden
		 * @return
		 */
		public function getHide():Boolean {
			return hide;
		}
		
	}
	
}